package com.xuanmiao.smsback.service.platform.operator.impl;

import com.panshi.hujin2.base.common.util.DozerUtils;
import com.panshi.hujin2.base.domain.page.Page;
import com.xuanmiao.smsback.dao.mapper.RechargeManageInfoMapper;
import com.xuanmiao.smsback.dao.mapper.UserAccountInfoMapper;
import com.xuanmiao.smsback.dao.mapper.UserInfoMapper;
import com.xuanmiao.smsback.dao.model.RechargeRecordInfoDO;
import com.xuanmiao.smsback.dao.model.UserInfoDO;
import com.xuanmiao.smsback.domain.qo.RechargeManageInfoQO;
import com.xuanmiao.smsback.model.RechargeRecordInfoBO;
import com.xuanmiao.smsback.service.platform.operator.RechargeManageInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service
public class RechargeManageInfoServiceImpl implements RechargeManageInfoService {
    @Autowired
    private RechargeManageInfoMapper rechargeManageInfoMapper;
    @Autowired
    private UserAccountInfoMapper userAccountInfoMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Override
    public List<RechargeRecordInfoBO> getList(RechargeManageInfoQO rechargeManageInfoQO) {
        if(rechargeManageInfoQO == null){
            rechargeManageInfoQO = new RechargeManageInfoQO();
        }
        //获取记录数量
        Integer count = rechargeManageInfoMapper.count(rechargeManageInfoQO);
        Page page = rechargeManageInfoQO.getPage();
        //设置总记录数
        page.setTotalNumber(count);
        List<RechargeRecordInfoDO> rechargeRecordInfoDO= rechargeManageInfoMapper.queryRechargeRecordInfo(rechargeManageInfoQO);
        if(rechargeRecordInfoDO!=null && rechargeRecordInfoDO.size()>0){
            List<RechargeRecordInfoBO> rechargeRecordInfoBO = DozerUtils.convertList(rechargeRecordInfoDO, RechargeRecordInfoBO.class);
            return rechargeRecordInfoBO;

        }
        return Collections.emptyList();


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(RechargeRecordInfoBO rechargeRecordInfoBO) {
     /*   if (!existBusinessNo(rechargeRecordInfoBO.getBusinessNo())){
            throw new RuntimeException("没有该商户号");
        }
        if (existTransactionId(rechargeRecordInfoBO.getTransactionId())){
            throw new RuntimeException("交易单号重复");
        }*/
        rechargeManageInfoMapper.save(DozerUtils.convert(rechargeRecordInfoBO, RechargeRecordInfoDO.class));
        userAccountInfoMapper.updateIncrease(DozerUtils.convert(rechargeRecordInfoBO, RechargeRecordInfoDO.class));


    }


    @Override
    public RechargeRecordInfoBO getById(Integer id) {
        return DozerUtils.convert(rechargeManageInfoMapper.getById(id),RechargeRecordInfoBO.class);
    }

    @Override
    public boolean existBusinessNo(String businessNo) {
        Integer res = userInfoMapper.existAccountNo(businessNo);
        if(res != null){
            return true;
        }else{
            return false;
        }

    }
    @Override
    public boolean existTransactionId(String transactionId) {
        Integer res = rechargeManageInfoMapper.existTransactionId(transactionId);
        if(res != null){
            return true;
        }else{
            return false;
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    //修改充值状态
    public void update(RechargeRecordInfoBO rechargeRecordInfoBO) {

        //获取原来的充值状态
        Integer res=rechargeManageInfoMapper.getStatus(DozerUtils.convert(rechargeRecordInfoBO, RechargeRecordInfoDO.class));
        //判断是否修改充值状态

          if (res==rechargeRecordInfoBO.getRechargeStatus()){
              //如果相同 不处理
          }else{
              //先修改充值信息 状态
              rechargeManageInfoMapper.update(DozerUtils.convert(rechargeRecordInfoBO, RechargeRecordInfoDO.class));
              //如果不相同 更新充值信息状态 再根据 状态变化 选择增加或者减少钱包余额

              if(res>rechargeRecordInfoBO.getRechargeStatus()){
                  //res=1 --成功改成失败

                  userAccountInfoMapper.updateReduce(DozerUtils.convert(rechargeRecordInfoBO,RechargeRecordInfoDO.class));
              }else{
                  userAccountInfoMapper.updateIncrease(DozerUtils.convert(rechargeRecordInfoBO,RechargeRecordInfoDO.class));
              }

          }

    }
}

